package com.healthcloud.rabbitmq.service;


import com.healthcloud.rabbitmq.entity.BrokerMessageLog;
import com.healthcloud.rabbitmq.entity.Order;
import com.healthcloud.dao.BrokerMessageLogMapper;
import com.healthcloud.dao.OrderMapper;
import com.healthcloud.utils.Constants;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class OrderService {

	@Autowired
	private OrderMapper orderMapper;
	
	@Autowired
	private BrokerMessageLogMapper brokerMessageLogMapper;
	
	
	public void createOrder(Order order, String messages) throws Exception {
//		Order order = new Order();
//		order.setId(new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"&" + UUID.randomUUID().toString());
//		order.setName(name);
//		order.setMessageId(System.currentTimeMillis() + "$" + UUID.randomUUID().toString());

		// order current time 
		Date orderTime = new Date();
		// order insert
//		orderMapper.insert(order);
		// log insert
		BrokerMessageLog brokerMessageLog = new BrokerMessageLog();
		brokerMessageLog.setMessageId(order.getMessageId());
		//save order message as json
		brokerMessageLog.setMessage(messages);
		brokerMessageLog.setStatus("0");
		brokerMessageLog.setNextRetry(DateUtils.addMinutes(orderTime, Constants.ORDER_TIMEOUT));
		brokerMessageLog.setCreateTime(new Date());
		brokerMessageLog.setUpdateTime(new Date());
//		brokerMessageLogMapper.insert(brokerMessageLog);

	}
	
}
